package com.wulin.wucut.domain.entity.draft.base;

import lombok.Data;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber;

@Data
public class VideoInfo {
    private FFmpegFrameGrabber grabber;

    public VideoInfo(String path) throws FrameGrabber.Exception {
        this.grabber = FFmpegFrameGrabber.createDefault(path);
        init();
    }
    
    private String path;

    private String name;

    private long duration;

    private long frames;

    private double frameRate;

    private Integer width;

    private Integer height;
    
    
    private void init() throws FrameGrabber.Exception {
        grabber.start();
        this.frames = grabber.getLengthInFrames();
        this.frameRate = grabber.getFrameRate();
        this.height = grabber.getImageHeight();
        this.width = grabber.getImageWidth();
        this.duration = computeDuration();
    }

    /**
     * 计算时长
     * 不保留小数，向上取整
     * duration = 1 * 1000* 1000 * frames  /frameRate 
     * @return
     */
    
    private long computeDuration() {
        return Math.round(1000 * 1000 * frames / frameRate);  
    }
    
    public long computeDuration(Double speed) {
        return Math.round(1000 * 1000 * frames /( frameRate *speed));  
    }

    
    
    
    
}
